package com.hyc.enmus;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public enum NewsCategoryEnum {
    NEWS(1, "新闻"),
    COMMODITY(2, "商品"),
    NOTICE(3, "公告"),
    IMG(4, "图片");

    // 编码
    private Integer code;
    // 名称
    private String desc;

    private NewsCategoryEnum(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public Integer getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }

    public static NewsCategoryEnum codeOf(Integer code) {
        return CODE_MAP.get(code);
    }

    private static final Map<Integer, NewsCategoryEnum> CODE_MAP = Collections.unmodifiableMap(initializeCodeMapping());

    private static Map<Integer, NewsCategoryEnum> initializeCodeMapping() {
        final Map<Integer, NewsCategoryEnum> codeMap = new HashMap<>();
        for (NewsCategoryEnum item : NewsCategoryEnum.values()) {
            codeMap.put(item.code, item);
        }
        return codeMap;
    }

    /**
     * 通过code获取枚举
     *
     * @param code
     */
    public static String getDescByCode(Integer code) {
        for (NewsCategoryEnum e : values()) {
            if (e.getCode().equals(code)) {
                return e.getDesc();
            }
        }
        return null;
    }
}
